#include "UserSettings.h"

#include "core/AppDirectory.h"
namespace {
const QString UserSettingsFilename = "user.settings.json";
}

UserSettings::UserSettings() {}

UserSettings& UserSettings::get()
{
    static UserSettings instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(
            AppDirectory::get().getAppDocFile(UserSettingsFilename)
        );
        instance.load();
    }
    return instance;
}

void UserSettings::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BaseDocument::fromJson(json, errorList);
    lastOpenDir_ = json.value("lastOpenDir").toString(lastOpenDir_);
}

QJsonObject UserSettings::toJson() const
{
    QJsonObject json = BaseDocument::toJson();

    json.insert("lastOpenDir", lastOpenDir_);

    return json;
}
